/* 
 * File:   Submission.cpp
 * Author: shemmenw
 * 
 * Created on August 3, 2011, 10:37 AM
 */

#include "Submission.h"
#include<Windows.h>
#include <stdio.h>

Submission::Submission()
{

}

Submission::Submission(const Submission& orig)
{

}

Submission::~Submission()
{

}

void Submission::loadCode()
{

}

void Submission::compile()
{

}

void Submission::execute()
{

}

void Submission::grade()
{

}

/*
 *FH****************************************************************************
 *FH
 *FHFN*	FUNCTION NAME: execProcessHandle
 *FH
 *FH		DESCRIPTION:
 *FH			Calls ExecuteProcess, converts string to std::wstring
 *FH			 for the call.
 *FH
 *FH****************************************************************************
 */
size_t execProcessHandle(
                         const std::string FullPathToExeStr,
                         const size_t SecondsToWait,
                         const std::string FullPathToOutput)
{
    std::wstring newFullPathToExe(FullPathToExeStr.length(), L' ');
    std::copy(FullPathToExeStr.begin(), FullPathToExeStr.end(), newFullPathToExe.begin());
    return ExecuteProcess(newFullPathToExe, SecondsToWait, FullPathToOutput);
} //execProcessHandle

/*
 *FH****************************************************************************
 *FH
 *FHFN*	FUNCTION NAME: ExecuteProcess
 *FH
 *FH		DESCRIPTION:
 *FH			  Calls a process to load the FullPathToExeStr file 
 *FH			   and send FullPathToOutput to output file.
 *FH			  Waits SecondsToWait seconds before terminating process
 *FH
 *FH****************************************************************************
 */
size_t ExecuteProcess(
                      const std::wstring FullPathToExe,
                      const size_t SecondsToWait,
                      const std::string FullPathToOutput)
{
    /* open output file to send student output */
    freopen(FullPathToOutput.c_str(), "w", stdout);
    size_t iMyCounter = 0, iReturnVal = 0, iPos = 0;
    DWORD dwExitCode = 0;

    /* CreateProcess API initialization */
    STARTUPINFOW siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof (siStartupInfo));
    memset(&piProcessInfo, 0, sizeof (piProcessInfo));
    siStartupInfo.cb = sizeof (siStartupInfo);

    if (CreateProcessW(const_cast<LPCWSTR> (FullPathToExe.c_str()),
                       NULL, 0, 0, true,
                       CREATE_DEFAULT_ERROR_MODE, 0, 0,
                       &siStartupInfo, &piProcessInfo) != false)
    {
        /* Watch the process. */
        dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000));
    }
    else
    {
        /* CreateProcess failed */
        iReturnVal = GetLastError();
    }

    /* kill the process if it was stopped due to time out */
    TerminateProcess(piProcessInfo.hProcess, 0);

    /* Release handles */
    CloseHandle(piProcessInfo.hProcess);
    CloseHandle(piProcessInfo.hThread);

    /* restore console stream */
    freopen("CON", "w", stdout);
    return iReturnVal;
} //ExecuteProcess